import { FairyGUI , System } from 'csharp';
import FairyUtils from '../../com/utils/FairyUtils';
/*
 *
 *@author clong 2020.11.01
 */
export default class BaseButton extends FairyGUI.GButton {

    protected _data = null;
    protected btns:Array<FairyGUI.GButton> = [];

    constructor() {
        super();
        Object.defineProperty(this, "data", {
            set:function(data){
                this.show( data );
            },
            get: function () { 
                return this._data;
            }
        });
    }

    public Setup_AfterAdd(buffer: FairyGUI.Utils.ByteBuffer, beginPos: number):void{

        super.Setup_AfterAdd(buffer,beginPos);

        FairyUtils.setVar( this , this , this.btns );   

        this.initUI();
    }

    initUI():void{

        
    }

    show( data:any ):void{

        this._data = data;
        this.visible = true;
        let btn:FairyGUI.GButton;
        for( btn of this.btns ){
            if(!btn) continue;
            let callback1 = (context:FairyGUI.EventContext)=>{
                this.onBtnClick( context.sender as FairyGUI.GButton );
            }
            btn && btn.onClick.Clear();
            btn && btn.onClick.Add( callback1 );
            this[btn.name + "_EventCallBack"] = callback1;
        }
    }

    onBtnClick( btn:any ):void{ 
        
    }

    hide():void{ 
        let btn:FairyGUI.GButton;
        for( btn of this.btns ){
            let callback1 = this[btn.name + "_EventCallBack"];
            btn && callback1 && btn.onClick.Remove( callback1 );
        }
        this.visible = false;
    }
}